/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.front.offlineComment;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.offline.OfflineService;
import shop.services.front.offline.bean.Offline;
import shop.services.front.offlineAccount.OfflineAccountService;
import shop.services.front.offlineAccount.bean.OfflineAccount;
import shop.services.front.offlineComment.OfflineCommentService;
import shop.services.front.offlineComment.bean.OfflineComment;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**   
 * @类名称：OfflineCommentController
 * @创建人：Ltz   
 * @创建时间：2017-08-07 下午14:56:32      
 * @版本号：1.0
 * @描述：  活动评价
 */
@Controller
@RequestMapping("/front/offlineComment")
public class OfflineCommentController extends FrontBaseController<OfflineComment> {
    @Resource
    private OfflineCommentService offlineCommentService;
    @Resource
    private OfflineAccountService offlineAccountService;
    @Resource
    private OfflineService offlineService;
    @Override
    public Services<OfflineComment> getService() {
        return offlineCommentService;
    }

    /**
     *跳转到公益日记页面
     * @return
     */
    @RequestMapping("/toPublicDiary")
    public String toPublicDiary() {
        return "/front/offlineComment/publicDiary";
    }

    /**
     *跳转到添加日记页面
     * @return
     */
    @RequestMapping("/toAddPublicDiary")
    public String toAddPublicDiary() {
        return "/front/offlineComment/addPublicDiary";
    }

    /**
     *跳转到编辑日记页面
     * @return
     */
    @RequestMapping("/toEditPublicDiary")
    public String toEditPublicDiary() {
        return "/front/offlineComment/editPublicDiary";
    }

    /**
     * 添加评价（活动）
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/insertOfflineComment", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertOfflineComment(HttpServletRequest request, @ModelAttribute("offlineComment") OfflineComment offlineComment) throws Exception {
        jsonResult = new JSONResult();
        List<OfflineComment> rsList=offlineCommentService.selectList(offlineComment);
        OfflineAccount offlineAccount=new OfflineAccount();
        offlineAccount.setAccountID(offlineComment.getAccountID());
        offlineAccount.setOfflineID(offlineComment.getOfflineID());
        OfflineAccount rs2 =offlineAccountService.selectOne(offlineAccount);//查询参与记录
        for(OfflineComment rs : rsList){//判断是否重复评论活动
            if(rs.getOfflineID().equals(offlineComment.getOfflineID())){
                jsonResult.setSuccess(false);
                jsonResult.setMessage("您已评价过该活动，请勿重复评论！");
                return jsonResult;
            }
        }
        rs2.setIsComment("y");//更新评论状态
        offlineAccountService.update(rs2);
        offlineCommentService.insert(offlineComment);
        Offline offline =new Offline();
        offline.setId(offlineComment.getOfflineID());
        offline=offlineService.selectOne(offline);
        offline.setCommentRecord(offline.getCommentRecord() + 1);
        offlineService.update(offline);//更新活动评论次数
        return jsonResult;
    }
}
